﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PaymentHelper.Core.Extension;
using System.Web.Routing;
using System.Web.Mvc;
using System.Web;
using PaymentHelper.Core.CommonUtils;

namespace PaymentHelper.Methods.BaoKim
{
    public class BaoKimProcessor : BaseExtension, IPaymentMethod
    {
        public bool SupportCapture
        {
            get { return false; }
        }

        public bool SupportRefund
        {
            get { return false; }
        }

        public bool SupportVoid
        {
            get { return false; }
        }

        public PaymentMethodType PaymentMethodType
        {
            get { return PaymentHelper.PaymentMethodType.Redirection; }
        }

        public decimal GetHandlingFee()
        {
            return GetSetting("HandlingFee");
        }

        public void ProcessPayment(ProcessPaymentRequest processPaymentRequest)
        {
            StringBuilder urlBuilder = new StringBuilder();
            if (GetSetting("UseSandbox") == true)
            {
                urlBuilder.Append(GetSetting("SandboxURL"));
            }
            else
            {
                urlBuilder.Append(GetSetting("BaoKimURL"));
            }

            urlBuilder.AppendFormat("?business={0}", GetSetting("Email"));
            urlBuilder.AppendFormat("&product_name={0}", processPaymentRequest.Order.OrderId);
            urlBuilder.AppendFormat("&price={0}&product_quantity=1&total_amount={1}", processPaymentRequest.Order.OrderTotal, processPaymentRequest.Order.OrderTotal);
            urlBuilder.AppendFormat("&url_success={0}", GetSetting("ReturnURL"));
            urlBuilder.AppendFormat("&url_cancel={0}", GetSetting("CancelURL"));
            urlBuilder.AppendFormat("&product_description={0}", "product description");

            HttpContext.Current.Response.Redirect(urlBuilder.ToString());
        }

        public CapturePaymentResult Capture(CapturePaymentRequest capturePaymentRequest)
        {
            CapturePaymentResult result = new CapturePaymentResult();
            result.AddError("Captured method not supported !");
            result.PaymentStatus = Core.RelatedObject.PaymentStatus.Canceled;
            result.Success = false;

            return result;
        }

        public RefundPaymentResult Refund(RefundPaymentRequest refundPaymentRequest)
        {
            RefundPaymentResult result = new RefundPaymentResult();
            result.AddError("Refund method not supported !");
            result.PaymentStatus = Core.RelatedObject.PaymentStatus.Canceled;
            result.Success = false;

            return result;
        }

        public VoidPaymentResult Void(VoidPaymentRequest voidPaymentRequest)
        {
            VoidPaymentResult result = new VoidPaymentResult();
            result.AddError("Void method not supported !");
            result.PaymentStatus = Core.RelatedObject.PaymentStatus.Canceled;
            result.Success = false;

            return result;
        }

        public void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
        {
            actionName = "Configure";
            controllerName = "BaoKim";
            routeValues = new RouteValueDictionary();
            routeValues.Add("Namespaces", "PaymentHelper.Methods.BaoKim.Controllers");
            routeValues.Add("area", null);
        }

        public void GetPaymentInfoRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
        {
            actionName = "PaymentInfo";
            controllerName = "BaoKim";
            routeValues = new RouteValueDictionary();
            routeValues.Add("Namespaces", "PaymentHelper.Methods.BaoKim.Controllers");
            routeValues.Add("area", null);
        }

        public void RegisterRoutes(RouteCollection routes)
        {
            routes.MapRoute(
                "BaoKim.PaymentInfo",
                "Methods/BaoKim/PaymentInfo",
                new { controller = "BaoKim", action = "PaymentInfo" },
                new[] { "PaymentHelper.Methods.BaoKim.Controllers" }
                );

            routes.MapRoute(
                "BaoKim.Configure",
                "Methods/BaoKim/Configure",
                new { controller = "BaoKim", action = "Configure" },
                new[] { "PaymentHelper.Methods.BaoKim.Controllers" }
                );
        }
        
        public void GetPaymentInfo(Core.Order orderCurrent, FormCollection collection)
        {
        }

        public PaymentHelper.Core.MVCUtils.PaymentHelperController GetControllerType()
        {
            return new BaoKim.Controllers.BaoKimController();
        }

        public bool AcceptCurrency(string currencyCode)
        {
            if(string.IsNullOrEmpty(currencyCode))
                return false;

            string currencySetting = GetSetting("CurrencyAccepted");

            if (string.IsNullOrEmpty(currencySetting))
                return false;

            var listCurrency = currencySetting.SplitToListString(',');
            return listCurrency.Contains(currencyCode);
        }
    }
}
